package com.weiyuwqj.app.mobile.sms;


import com.weiyuwqj.api.constant.Constants;
import com.weiyuwqj.api.util.Sms;
import com.weiyuwqj.api.service.SysNoticeTypeService;
import com.weiyuwqj.api.service.SysTempcodeService;
import com.weiyuwqj.api.service.BasUserService;
import com.weiyuwqj.api.exception.CrowdException;
import com.weiyuwqj.api.model.BasUser;
import com.weiyuwqj.api.model.SysNoticeType;
import com.weiyuwqj.api.model.SysTempcode;
import com.weiyuwqj.api.util.IpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhangyong on 2016/8/19.
 */
@Controller
public class MobileSmsController {

    @Autowired
    private SysNoticeTypeService noticeTypeService;

    @Autowired
    private SysTempcodeService tempcodeService;

    @Autowired
    private BasUserService userService;

    private Map smsInfo(String vaildPhoneCode, String mobile, SysNoticeType noticeType) {
        String content = noticeType.getTypeContent();
        System.out.println("您的验证码：" + vaildPhoneCode);
        content = content.replace(Constants.VALIDATECODE, vaildPhoneCode);
        String smsResponse = Sms.send(mobile, content).toString();

        Map<String, Object> ret = new HashMap<String, Object>();
        if ("发送成功".equals(smsResponse)) {
            ret.put("result", "success");
            ret.put("validPhoneExpiredTime", new Date());
        } else {
            ret.put("result", "fail");
            ret.put("message", smsResponse);
        }
        return ret;
    }

    //注册短信
    @ResponseBody
    @RequestMapping(value = "/mobile/user/registerSendSms")
    public Object registerSend(HttpServletRequest request) {
        String vaildPhoneCode = request.getParameter("vaildPhoneCode");
        String mobile = request.getParameter("mobile");
        BasUser user = userService.getUserByMobile(mobile);
        if (user != null) {
            throw new CrowdException("sys.user.add.user_exist");
        }
        SysTempcode tempcode = new SysTempcode();
        tempcode.setAddtime(new Date());
        tempcode.setAddip(IpUtils.get());
        tempcode.setCode(vaildPhoneCode);
        tempcode.setMobile(mobile);
        tempcodeService.save(tempcode);
        SysNoticeType noticeType = noticeTypeService.getNoticeTypeByNid(Constants.reg_validatecode_sms);
        return smsInfo(vaildPhoneCode, mobile, noticeType);
    }

    //登录短信
    @ResponseBody
    @RequestMapping(value = "/mobile/user/doLoginSendSms")
    public Object doLoginSend(HttpServletRequest request) {
        String vaildPhoneCode = request.getParameter("vaildPhoneCode");
        String mobile = request.getParameter("mobile");
        SysTempcode tempcode = new SysTempcode();
        tempcode.setAddtime(new Date());
        tempcode.setAddip(IpUtils.get());
        tempcode.setCode(vaildPhoneCode);
        tempcode.setMobile(mobile);
        tempcodeService.save(tempcode);
        SysNoticeType noticeType = noticeTypeService.getNoticeTypeByNid("login_sms");
        return smsInfo(vaildPhoneCode, mobile, noticeType);
    }

    /**
     * 修改绑定旧手机短信
     *
     * @param userId         用户ID
     * @param vaildPhoneCode 验证码
     * @return 发送结果
     */
    @ResponseBody
    @RequestMapping(value = "/mobile/user/oldModifyMobileSms")
    public Object oldModifyMobileSms(Integer userId, String vaildPhoneCode) {
        BasUser user = userService.selectByKey(userId);
        SysNoticeType noticeType = noticeTypeService.getNoticeTypeByNid("modifymobile_sms");
        return smsInfo(vaildPhoneCode, user.getMobile(), noticeType);
    }

    /**
     * 修改绑定新手机短信
     * @param mobile         新手机号码
     * @param vaildPhoneCode 验证码
     * @return 发送结果
     */
    @ResponseBody
    @RequestMapping(value = "/mobile/user/newModifyMobileSms")
    public Object newModifyMobileSms(String mobile, String vaildPhoneCode) {
        SysNoticeType noticeType = noticeTypeService.getNoticeTypeByNid("modifymobile_sms");
        SysTempcode tempcode = new SysTempcode();
        tempcode.setAddtime(new Date());
        tempcode.setAddip(IpUtils.get());
        tempcode.setCode(vaildPhoneCode);
        tempcode.setMobile(mobile);
        tempcodeService.save(tempcode);
        return smsInfo(vaildPhoneCode, mobile, noticeType);
    }


    //找回密码
    @ResponseBody
    @RequestMapping(value = "/mobile/user/backPasswordSms")
    public Object backPasswordSms(HttpServletRequest request) {
        String vaildPhoneCode = request.getParameter("vaildPhoneCode");
        String mobile = request.getParameter("mobile");
        BasUser user = userService.getUserByMobile(mobile);
        if(user == null){
            throw new CrowdException("user.forget.user_not_exist");
        }
        SysNoticeType noticeType = noticeTypeService.getNoticeTypeByNid("forgetpass_sms");
        return smsInfo(vaildPhoneCode, mobile, noticeType);
    }


    //自动投标验证短信
    @ResponseBody
    @RequestMapping(value = "/mobile/user/autoSms")
    public Object autoSms(HttpServletRequest request) {
        String vaildPhoneCode = request.getParameter("vaildPhoneCode");
        Integer id = Integer.valueOf(request.getParameter("userId"));
        BasUser user = userService.selectByKey(id);
        SysNoticeType noticeType = noticeTypeService.getNoticeTypeByNid("auto_sms");
        return smsInfo(vaildPhoneCode, user.getMobile(), noticeType);
    }

    //修改密码短信
    @ResponseBody
    @RequestMapping(value = "/mobile/user/modifyPasswordSms")
    public Object modifyPasswordSms(HttpServletRequest request) {
        String vaildPhoneCode = request.getParameter("vaildPhoneCode");
        Integer id = Integer.valueOf(request.getParameter("userId"));
        BasUser user = userService.selectByKey(id);
        if(user == null){
            throw new CrowdException("user.forget.user_not_exist");
        }
        SysNoticeType noticeType = noticeTypeService.getNoticeTypeByNid("modifypass_sms");
        return smsInfo(vaildPhoneCode, user.getMobile(), noticeType);
    }



}
